package cn.mrlong.mdialogstyle.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.icu.util.Calendar;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import cn.mrlong.mdialogstyle.R;

public class AlertDateATimeDialog {
    private Context context;
    private Dialog dialog;
    private Display display;
    private PostClickListener listener;
    private boolean isShowTime = true;//是否显示时间
    private boolean isWhite = false;//默认黑色
    private DatePicker datePicker;
    private TimePicker timePicker;
    private String title;
    private TextView title1;

    public AlertDateATimeDialog setTitle(String title) {
        this.title = title;
        if (null != title && !"".equals(title))
            title1.setText(title);
        return this;
    }

    public AlertDateATimeDialog(Context context, boolean isShowTime, boolean isWhite) {
        this.isShowTime = isShowTime;
        this.context = context;
        this.isWhite = isWhite;
    }

    public AlertDateATimeDialog builder() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = wm.getDefaultDisplay();
        //加载布局
        View view;
        if (isWhite) {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_date_time_white, null);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_date_time, null);
        }

        TextView cancel = view.findViewById(R.id.tv_cancel);
        title1 = view.findViewById(R.id.tv_title);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog) {
                    dialog.dismiss();
                }
            }
        });
        TextView sure = view.findViewById(R.id.tv_sure);
        datePicker = view.findViewById(R.id.dp_date);
        timePicker = view.findViewById(R.id.dp_time);


        //控制显示
        timePicker.setIs24HourView(true);
        datePicker.setCalendarViewShown(false);
        datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        timePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        if (!isShowTime) {
            timePicker.setVisibility(View.GONE);
        } else {
            timePicker.setVisibility(View.VISIBLE);
        }
        resizePikcer(datePicker);
        resizePikcer(timePicker);
        //定义dialog
        view.setMinimumWidth(display.getWidth());
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = 0;
        window.setAttributes(lp);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String dAt = "";
                String timeStamp = "";//时间戳对象
                if (isShowTime) {//显示全部
                    //获得时间戳
                    timeStamp = TimeUtil.data(datePicker.getYear() + "年" + (datePicker.getMonth() + 1) + "月" + datePicker.getDayOfMonth() + "日"
                            + timePicker.getCurrentHour() + "时" + timePicker.getCurrentMinute() + "分" + "00秒");
                    if (delimiter.length > 0) {
                        dAt = datePicker.getYear() + delimiter[0] + (datePicker.getMonth() + 1) + delimiter[1] + datePicker.getDayOfMonth() + delimiter[2]
                                + timePicker.getCurrentHour() + delimiter[3] + timePicker.getCurrentMinute() + delimiter[4];
                    } else if (!"".equals(delimiterStr) && !"".equals(delimiterStr)) {
                        dAt = datePicker.getYear() + delimiterStr + (datePicker.getMonth() + 1) + delimiterStr + datePicker.getDayOfMonth() + delimiterStr
                                + timePicker.getCurrentHour() + delimiterStr + timePicker.getCurrentMinute();
                    } else {
                        dAt = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth() + "-"
                                + timePicker.getCurrentHour() + "-" + timePicker.getCurrentMinute();
                    }
                } else {
                    if (delimiter.length > 0) {
                        dAt = datePicker.getYear() + delimiter[0] + (datePicker.getMonth() + 1) + delimiter[1] + datePicker.getDayOfMonth() + delimiter[2];
                    } else if (!"".equals(delimiterStr) && !"".equals(delimiterStr)) {
                        dAt = datePicker.getYear() + delimiterStr + (datePicker.getMonth() + 1) + delimiterStr + datePicker.getDayOfMonth();
                    } else {
                        dAt = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth();
                    }
                }
                if (null != listener) {
                    System.out.println("====>" + timeStamp);
                    listener.onClick(dAt, timeStamp);
                }
                dialog.dismiss();
            }
        });
        return this;
    }

    private void resizePikcer(FrameLayout tp) {
        List<NumberPicker> npList = findNumberPicker(tp);
        for (NumberPicker np : npList) {
            resizeNumberPicker(np);
        }
    }

    private String[] delimiter = new String[]{};
    private String delimiterStr;
    private long maxData = 0;
    private long minData = 0;

    public AlertDateATimeDialog setMaxDate(long timeStamp) {
        this.maxData = timeStamp;
        if (maxData != 0) {
            datePicker.setMaxDate(maxData);
        }
        return this;
    }

    public AlertDateATimeDialog setMinDate(long timeStamp) {
        this.minData = timeStamp;
        if (minData != 0) {
            datePicker.setMaxDate(timeStamp);
        }
        return this;
    }

    public AlertDateATimeDialog setDelimiter(String[] delimiter) {
        this.delimiter = delimiter;
        return this;
    }

    public AlertDateATimeDialog setDelimiter(String delimiter) {
        this.delimiterStr = delimiter;
        return this;
    }

    public AlertDateATimeDialog setPostClickListener(PostClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface PostClickListener {
        void onClick(String dateAndTime, String timeStamp);
    }

    private void setNumberPickerTextSize(ViewGroup viewGroup) {
        List<NumberPicker> npList = findNumberPicker(viewGroup);
        if (null != npList) {
            for (NumberPicker np : npList) {
                EditText et = findEditText(np);
                et.setFocusable(false);
                et.setGravity(Gravity.CENTER);
                et.setTextSize(10);

            }
        }
    }

    private EditText findEditText(NumberPicker np) {
        if (null != np) {
            for (int i = 0; i < np.getChildCount(); i++) {
                View child = np.getChildAt(i);

                if (child instanceof EditText) {
                    return (EditText) child;
                }
            }
        }

        return null;
    }

    /*
     * 调整numberpicker大小
     */
    private void resizeNumberPicker(NumberPicker np) {
        LinearLayout.LayoutParams params;
        if (!isShowTime) {
            params = new LinearLayout.LayoutParams(display.getWidth() / 4, FrameLayout.LayoutParams.WRAP_CONTENT);
        } else {
            params = new LinearLayout.LayoutParams(display.getWidth() / 7, FrameLayout.LayoutParams.WRAP_CONTENT);
        }
        params.setMargins(10, 0, 10, 0);
        np.setLayoutParams(params);
    }

    private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
        List<NumberPicker> npList = new ArrayList<NumberPicker>();
        View child = null;
        if (null != viewGroup) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                child = viewGroup.getChildAt(i);
                if (child instanceof NumberPicker) {
                    npList.add((NumberPicker) child);
                } else if (child instanceof LinearLayout) {
                    List<NumberPicker> result = findNumberPicker((ViewGroup) child);
                    if (result.size() > 0) {
                        return result;
                    }
                }
            }
        }
        return npList;
    }

    public void show() {
        dialog.show();
    }


}
